﻿
namespace Patterns4Net.ArchitectureExplorer.Diagram
{
    using System.Collections.Generic;
    using System.Linq;

    public class Diagram : DiagramElement
    {
        private readonly IList<Layer> layers = new List<Layer>();

        private readonly IList<Relation> relations = new List<Relation>();

        public Diagram() : base("Diagram")
        {
            this.Parent = this;
        }

        public IEnumerable<Layer> Layers
        {
            get { return this.layers; }
        }

        public IEnumerable<Relation> Relations
        {
            get { return this.relations; }
        }

        public IEnumerable<TypeInfo> Types
        {
            get { return this.layers.SelectMany(layer => layer.Packages.SelectMany(package => package.Types)); }
        }

        public void AddLayer(Layer layer)
        {
            layer.Parent = this;
            this.layers.Add(layer);
        }

        public void AddRelation(Relation relation)
        {
            this.relations.Add(relation);
        }

        public void RemoveRelation(Relation relation)
        {
            this.relations.Remove(relation);
        }

        public IEnumerable<Relation> GetRelations(DiagramElement diagramElement)
        {
            return this.relations.Where(r => r.Source == diagramElement);
        }

        public IEnumerable<Relation> GetRelations(DiagramElement source, DiagramElement target)
        {
            return this.relations.Where(r => r.Source == source && r.Target == target);
        }

        public override void Accept(DiagramVisitor visitor)
        {
            visitor.Visit(this);
            foreach (var layer in this.Layers)
            {
                layer.Accept(visitor);
            }
        }
    }
}
